const KoaRouter = require('koa-router');
const Template = require('../util/Template');
const HttpClient = require('../util/HttpClient')

const modules = require('../../modules');
const router = new KoaRouter();

router.get('/security/department', async ctx => {
  let modConfig = modules['dept.view'];
  let reqHeaders = ctx.request.headers;
  let script = ['/dept.view.js'];
  let result = await HttpClient.request('/security/department', 'get', null, { headers: reqHeaders });
  let body = result.body;

  ctx.body = await Template.renderWithHtml('组织架构', body, script, modConfig.template, ctx)
})

router.get('/security/role/view', async ctx => {
  let modConfig = modules['role.view'];
  let reqHeaders = ctx.request.headers;
  let script = ['/role.view.js'];
  let result = await HttpClient.request('/security/role/view', 'get', null, { headers: reqHeaders });
  let body = result.body;
  ctx.body = await Template.renderWithHtml('角色管理', body, script, modConfig.template, ctx)
})

router.get('/security/roleService/view', async ctx => {
  let modConfig = modules['roleService.view'];
  let reqHeaders = ctx.request.headers;
  let script = ['/roleService.view.js'];
  let result = await HttpClient.request('/security/role/view', 'get', null, { headers: reqHeaders });
  let body = result.body;
  ctx.body = await Template.renderWithHtml('服务商权限管理', body, script, modConfig.template, ctx)
})

// 新建角色
router.get('/security/role/create', async ctx => {
  let modConfig = modules['role.detail'];
  let reqHeaders = ctx.request.headers;
  let script = ['/role.detail.js']
  let result = await HttpClient.request('/security/role/create', 'get', null, { headers: reqHeaders });
  let body = result.body;
  ctx.body = await Template.renderWithHtml('新建角色', body, script, modConfig.template, ctx)
})

// 详情角色 /security/role/view/2
router.get('/security/role/view/:id', async ctx => {
  let modConfig = modules['role.detail'];
  let reqHeaders = ctx.request.headers;
  let script = ['/role.detail.js']
  let result = await HttpClient.request(`/security/role/view/${ctx.params.id}`, 'get', null, { headers: reqHeaders });
  let body = result.body;
  ctx.body = await Template.renderWithHtml('角色详情', body, script, modConfig.template, ctx)
})

// 编辑角色 /security/role/edit/2
router.get('/security/role/edit/:id', async ctx => {
  let modConfig = modules['role.detail'];
  let reqHeaders = ctx.request.headers;
  let script = ['/role.detail.js']
  let result = await HttpClient.request(`/security/role/edit/${ctx.params.id}`, 'get', null, { headers: reqHeaders });
  let body = result.body;
  ctx.body = await Template.renderWithHtml('编辑角色', body, script, modConfig.template, ctx)
})

// 部门主页
router.get('/security/tag/home/:id', async ctx => {
  let modConfig = modules['security.tag.home'];
  let reqHeaders = ctx.request.headers;
  let script = ['/security.tag.home.js'];
  let result = await HttpClient.request(`/security/tag/home/${ctx.params.id}`, 'get', null, { headers: reqHeaders });
  let body = result.body;

  ctx.body = await Template.renderWithHtml('部门主页', body, script, modConfig.template, ctx)
})

// 个人详情
router.get('/mine/:id', async ctx => {
  let modConfig = modules['user.view'];
  let reqHeaders = ctx.request.headers;
  let script = ['/user.view.js'];
  let result = await HttpClient.request(`/shb/home/mine/${ctx.params.id}`, 'get', null, { headers: reqHeaders });
  let body = result.body;
  ctx.body = await Template.renderWithHtml('成员详情', body, script, modConfig.template, ctx)
})

// 用户详情
router.get('/security/user/view/:id', async ctx => {
  console.log('ctx', ctx.params.id, ctx);
  let modConfig = modules['user.view'];
  let reqHeaders = ctx.request.headers;
  let script = ['/user.view.js'];
  let result = await HttpClient.request(`/security/user/view/${ctx.params.id}`, 'get', null, { headers: reqHeaders });
  let body = result.body;
  ctx.body = await Template.renderWithHtml('成员详情', body, script, modConfig.template, ctx)
})

module.exports = router;